/*************************************************
 *FileName:      withdraw.js
 *Description:   提现.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token,balance,max_withdrawals,rates;
    var oMoney,oname,ocardid,oWechat,oAlipay;
    var servicecharge;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //加载
        lode();
        //查看提现状态
        lodepsd();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.useragrre').on('tap',function(){
            $(this).toggleClass('active');
        })//协议
        
        $('.useragrre-a').on('tap',function(event){
            common.pageTransition('useragrre.html?that=withdraw')
            return false;
        })
        $('.withdraw .payment a').on('tap',function(){//单选，提现到微信或者支付宝
            $(this).find('span').addClass('checked').parents('a').siblings().find('span').removeClass('checked');
            var oInput=$(this).siblings('a').find('input').val('');
            if(oInput){
                $('.withdraw button').removeClass('btn');
            }
        })
        
        $('.useragrre').on('tap',function(){
            judge();
        })
        
        $('.table-view1 .table-view-cell-a').on('tap',function(){//input有值的时候才可以确定
            $('.table-view1').css({display:'none'})
            $('.popover-img').css({display:'none'})
        })
           
        
        $('.table-view1 .table-view-cell-affirm').on('tap',function(){
            var oext =$('.table-view-cell-input').val();
            $('.table-view-cell-input').val('');
            postsub(oext);
        })
        
        $('.withdraw input').on('input',function(){//input有值的时候才可以确定
            judge();
        })
        
        function judge(){
            oMoney=$('.withdraw .money').val();
            $('.servicecharge').text(''+(servicecharge*oMoney/100)+'换币');
            oname=$('.withdraw .payment input').eq(0).val();
            ocardid=$('.withdraw .payment input').eq(1).val();
            oWechat=$('.wechat-box').find('input').val();
            oAlipay=$('.alipay-box').find('input').val();
            $('.withdraw button')[ ocardid!=''&&oname!=''&& oMoney!=''&&(oWechat||oAlipay)&& $('.useragrre').hasClass('active') ?'addClass':'removeClass']('btn');  
        }//判断是否全写
        
        $('.withdraw').on('tap','.btn',function(){
            var shu =$('.money').val()/rates;
            var banduan = max_withdrawals ? shu>max_withdrawals : max_withdrawals;
            var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
            if(!reg.test(ocardid)){
                mui.toast('身份证输入不合法！');
                return false;
            }else if(shu>balance){
                mui.toast('勋章数量不够！');
                return false;
            }else if(banduan){
                mui.toast('超过个人最大提现数量！')
                return false;
            }else{
                mui.confirm('姓名：'+oname+'<br />身份证：'+ocardid+'<br />提现'+[oWechat ?'微信号：'+oWechat :'支付宝账号：'+oAlipay]+'','提示',['确认提现','取消'],function(e){
                    if(e.index==0){
                        $('.table-view1').css({display:'block'})
                        $('.popover-img').css({display:'block'})
                    }
                },'div')
            }
        }) 
        
        $('.money').on('input',function(){
            var obj = $('.money').val();
            obj = obj.replace(/[^\d.]/g,"");
            obj = obj.replace(/^\./g,""); 
            obj = obj.replace(/\.{2,}/g,"."); 
            obj = obj.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj = obj.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            $('.money').val(obj);
            judge();
        })
    }
    /**
     * lodepsd
     * 查看提现状态
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function lodepsd(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/isSetWithdrawPassword",
            dataType:'jsonp',
            data: {uid:uid,token:token},
            success: function(data) {
                if(data.error_code == 0){
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else if(data.error_code == 1){
                    mui.alert('<div class="cent">您还未设置提现密码，请设置提现密码!</div>','提示','确定',function(){
                        common.pageTransition("withdrawalspsd.html");
                    },'div')
                }else if(data.error_code == 2){
                    mui.confirm('<div class="cent">您还不是VIP,是否</br>开通会员!</div>','提示',['确定','取消'],function(e){
                        e.index==0 ? common.pageTransition("membercenter.html") : setTimeout(function(){mui.back()},500)
                    },'div')
                }
            }  
        });
    }
    /**
     * lode
     * 加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function lode(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/withdrawalsInfo",
            dataType:'jsonp',
            data: {uid:uid,token:token},
            success: function(data) {
                if(data.error_code == 0){
                    balance=data.data.balance;
                    rates=data.data.rates;
                    max_withdrawals=data.data.max_withdrawals;
                    $('.withdraw-num').text(data.data.balance)
                    servicecharge=data.data.service_charge;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                }
            }  
        });
    }
    
    /**
     * postsub
     * 提现
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function postsub(oext){
        var account_number = oWechat? oWechat :oAlipay;
        var account_type =oWechat? 'weixin' :'alipay';
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/withdrawals",
            dataType:'jsonp',
            data: {uid:uid,token:token,id_card:ocardid,account_type:account_type,account_number:account_number,name:oname,money:oMoney,withdraw_password:oext},
            success: function(data) {
                if(data.error_code == 0){
                    mui.toast('提现成功！',{ duration:2000, type:'div' });
                    setTimeout(function(){mui.back();},2000)
                    var list = plus.webview.currentWebview().opener();//触发父页面的自定义事件(notecase2),从而进行刷新
                    mui.fire(list, 'notecase2');//返回true,继续页面关闭逻辑
                    return true;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg)
                }
            }  
        });
    }